package com.masterclient.app.modules.goods.newpage.adapter;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;
import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.masterclient.app.BaseActivity;
import com.masterclient.app.R;
import com.masterclient.app.base.MainViewHolder;
import com.eoner.baselibrary.bean.goods.CommodityDetailData;
import com.masterclient.app.utils.ImageTools;
import com.eoner.baselibrary.utils.ViewTransformUtil;

public class NewCommodityDetailDescriptionAdapter extends DelegateAdapter.Adapter<MainViewHolder> {

    Activity mContext;

    private LayoutHelper mLayoutHelper;


    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private int mCount = 0;

    CommodityDetailData data;


    public NewCommodityDetailDescriptionAdapter(Activity context, LayoutHelper layoutHelper, int count) {
        this(context, layoutHelper, count, null,null);
    }

    public NewCommodityDetailDescriptionAdapter(Activity context, LayoutHelper layoutHelper, int count, @NonNull VirtualLayoutManager.LayoutParams layoutParams, CommodityDetailData data) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.mCount = count;
        this.mLayoutParams = layoutParams;
        this.data = data;
    }
    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }
    @Override
    protected void onBindViewHolderWithOffset(MainViewHolder holder, int position, int offsetTotal) {

    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return null;
    }
    @Override
    public void onBindViewHolder(MainViewHolder holder, int position) {
        if(mLayoutParams!=null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }

        WebView webView = holder.itemView.findViewById(R.id.webView);
        ImageView introduce_image = holder.itemView.findViewById(R.id.introduce_image);
        ImageView seller_img_notice = holder.itemView.findViewById(R.id.seller_img_notice);

        if (data.getSeller() != null && !TextUtils.isEmpty(data.getSeller().getSeller_notice())) {
            seller_img_notice.setVisibility(View.VISIBLE);
            ViewTransformUtil.glideImageView(mContext, data.getSeller().getSeller_notice(), seller_img_notice, R.mipmap.defaults);
        } else {
            seller_img_notice.setVisibility(View.GONE);
        }

        String topHtml = "<head><meta charset=\"utf-8\"><meta name=\"viewport\" " +
                "content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, " +
                "user-scalable=0\"><title></title><style>body{margin:0;padding:0}p{margin:0}" +
                "img{display:inline-block;width:100%;line-height:0;}</style></head><body>" +
                "<base href=\"http://www.shihanbainian.com/\"/></body>";

        String html = topHtml.replace("<body><base href=\"http://www.shihanbainian.com/\"/></body>",
                "<body><base href=\"http://www.shihanbainian.com/\"/>" + data.getDesc_image() + "</body>");
        webView.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);

        ImageTools.adaptImageView((BaseActivity) mContext, introduce_image);
        if (!TextUtils.isEmpty(data.getIntroduce_image())) {
            introduce_image.setVisibility(View.VISIBLE);
            ViewTransformUtil.glideImageView(mContext, data.getIntroduce_image(), introduce_image, R.mipmap.defaults);
        } else {
            introduce_image.setVisibility(View.GONE);
        }

    }


    @Override
    public int getItemCount() {
        return mCount;
    }

    @Override
    public int getItemViewType(int position) {
        return 6;
    }
}
